{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b369079a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d4bd0afd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "69829dc9",
   "metadata": {},
   "outputs": [],
   "source": [
    "df =pd.DataFrame(data=np.random.randint(0, 1000, (200, 500000)),\n",
    "columns=range(500000), index=range(200))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5bb9c3b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def top_k(x,k):\n",
    "    ind=np.argpartition(x,-1*k)[-1*k:]\n",
    "    return ind[np.argsort(x[ind])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c7871372",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\vnstudio\\lib\\site-packages\\ipykernel_launcher.py:1: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 39, 153,  85, ...,  62,  37,  89],\n",
       "       [132, 196, 130, ...,  81,  10,  69],\n",
       "       [102,  11, 152, ..., 156, 179,  28],\n",
       "       ...,\n",
       "       [  3, 116, 195, ..., 197,  91,  37],\n",
       "       [ 42, 169, 167, ...,   8,  28, 160],\n",
       "       [ 12, 174, 113, ...,  94,  71, 188]], dtype=int64)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.apply_along_axis(lambda x: top_k(x,10),0,df.as_matrix())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1e94ccca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     0       1       2       3       4       5       6       7       8       \\\n",
      "0       753      65     557     332     477      83     346     150     340   \n",
      "1       161      26     494     468     749     989       3     363     452   \n",
      "2       550     339     317     941     866     545     215     989      93   \n",
      "3       988     156      26     962     192     549     610     736     579   \n",
      "4       444     456     607     438     542     672     371     258     406   \n",
      "..      ...     ...     ...     ...     ...     ...     ...     ...     ...   \n",
      "195     669      42     991     205     975     975     754     782     863   \n",
      "196     475     951     718     587     841     827     581     516     638   \n",
      "197     337     676     654     855     515     762     499     206     269   \n",
      "198     504     741     231     418     989     189     191     959     694   \n",
      "199     155     872     214     568     729     668     592     903     606   \n",
      "\n",
      "     9       ...  499990  499991  499992  499993  499994  499995  499996  \\\n",
      "0       111  ...     126      69     210     505     450     978     711   \n",
      "1       844  ...     331     929     973     582      71     573     474   \n",
      "2       601  ...     865     429     649     759     678     152     797   \n",
      "3       228  ...     458     203     888     350     113     347     808   \n",
      "4       886  ...     355     617     801     903     595     124     754   \n",
      "..      ...  ...     ...     ...     ...     ...     ...     ...     ...   \n",
      "195     960  ...     490      45     563     729     376     988     723   \n",
      "196     473  ...     282     649     719     771      98     334     405   \n",
      "197     360  ...     652     120     160     821     973     961     806   \n",
      "198     421  ...     552     608     264      97     640     251     372   \n",
      "199     108  ...     347     684     751       8     214     978     805   \n",
      "\n",
      "     499997  499998  499999  \n",
      "0       945     834     107  \n",
      "1       665     577     518  \n",
      "2       544     951     208  \n",
      "3       847     906      87  \n",
      "4         8     835     858  \n",
      "..      ...     ...     ...  \n",
      "195     447     919     754  \n",
      "196     893      43      19  \n",
      "197     968     688     248  \n",
      "198     264     125     371  \n",
      "199     323     155     848  \n",
      "\n",
      "[200 rows x 500000 columns]\n"
     ]
    }
   ],
   "source": [
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a39206c1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
